package com.myplans.server.service;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.MySQLJDBCIDMigrator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.recommender.CachingRecommender;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.recommender.slopeone.SlopeOneRecommender;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.model.JDBCDataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.myplans.client.service.PlanService;
import com.myplans.criteria.CategorySearchCriteria;
import com.myplans.criteria.PlanSearchCriteria;
import com.myplans.server.util.ServiceLoader;
import com.myplans.shared.Category;
import com.myplans.shared.Plan;
import com.myplans.shared.User;
import com.myplans.vo.CategoryValue;
import com.myplans.vo.PlanValue;
import com.myplans.vo.UserValue;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class PlanServiceImpl extends RemoteServiceServlet implements PlanService {

	@Override
	public Plan addPlan(Plan plan) {
		PlanValue planValue = new PlanValue();
		try {
			BeanUtils.copyProperties(planValue, plan);
			planValue = ServiceLoader.getServiceInstance().getPlanService().createPlan(planValue);
			BeanUtils.copyProperties(plan, planValue);
			
			return plan;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Plan> getPlans(String categoryName, String searchString) {
		PlanSearchCriteria searchCriteria = new PlanSearchCriteria();
		if (categoryName != null) {
			searchCriteria.setCategory(categoryName);
		}
		if (searchString != null) {
			searchCriteria.setCategory(searchString);
			searchCriteria.setLocation(searchString);
			searchCriteria.setDescription(searchString);
			searchCriteria.setName(searchString);
		}
		List<PlanValue> planValues = ServiceLoader.getServiceInstance().
				getPlanService().getPlans(searchCriteria);
		List<Plan> plans = new ArrayList<Plan>();
		
		try {
			for (PlanValue planValue : planValues) {
				Plan plan = new Plan();
				BeanUtils.copyProperties(plan, planValue);
				List<User> users = new ArrayList<User>();
				for (UserValue userValue : planValue.getParticipants()) {
					User participant = new User();
					BeanUtils.copyProperties(participant, userValue);
					users.add(participant);
				}
				plan.setAttendingUsers(users);
				plans.add(plan);
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return plans;
	}
	
	@Override
	public List<Category> getCategories() {
		CategorySearchCriteria searchCriteria = new CategorySearchCriteria();
		List<CategoryValue> planValues = ServiceLoader.getServiceInstance().
				getPlanService().getCategories(searchCriteria);
		List<Category> categories = new ArrayList<Category>();
		
		try {
			for (CategoryValue categoryValue : planValues) {
				Category category = new Category();
				BeanUtils.copyProperties(category, categoryValue);
				categories.add(category);
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return categories;
	}

}
